package com.fengye.algorithms.leecode;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author fengye
 * @Date 2023/4/8 11:45
 **/
public class HappyNumber202 {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<Integer>();
        while(n != 1 && !set.contains(n)) {
            set.add(n);
            n = doHappy(n);
        }

        return n == 1;
    }

    public int doHappy(int n) {
        int m = 0;
        while(n != 0) {
            int t = n % 10;
            n /= 10;
            m += t * t;
        }

        return m;
    }
}
